/*
 * TCSS 360 - Winter 2014
 * TESK Software
 */
package view.Papers;

import javax.swing.JPanel;
import javax.swing.JLabel;

import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import java.awt.Font;
import javax.swing.SwingConstants;
import java.awt.Color;
import javax.swing.JButton;

import view.MainFrame;
import view.userProfile.AuthorView;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import javax.swing.JTextField;

import javax.swing.border.TitledBorder;
import javax.swing.border.CompoundBorder;
import javax.swing.border.BevelBorder;
import javax.swing.border.EtchedBorder;

/**
 * The GUI for author when he clicks on submit paper button.
 * @author Kon 
 * @author Thang Submit/Upload
 * @author Emily, edit the view changes.
 * @version 3/5/14
 */
@SuppressWarnings("serial")
public class SubmitPaperView extends JPanel{
	
	/** The frame the panel is associated with. */
	private MainFrame my_frame;
	
	/** The previous view the frame was associated with. */
	private AuthorView my_view;
	
	/** The combobox of conferences that can be selected. */
	private JComboBox cfList;
	
	/** The name of the uploaded file in a JLabel. */
	private JLabel uploadedFileName;
	
	/** The file that is being uploaded. */
	protected File file;
	
	/** The text field for the author to enter the title name. */
	private JTextField textField;
	
	/**
	 * Constructs a view for an author to submit a paper.
	 * 
	 * @param the_frame the frame to associate the panel with
	 * @param the_view the view that was previously associated with the frame
	 */
	public SubmitPaperView(MainFrame the_frame, AuthorView the_view) {
		setBorder(new TitledBorder(new CompoundBorder(new BevelBorder(BevelBorder.LOWERED, new Color(148, 0, 211), null, null, null), new EtchedBorder(EtchedBorder.LOWERED, new Color(221, 160, 221), new Color(152, 251, 152))), "Submit a Paper", TitledBorder.LEFT, TitledBorder.TOP, null, new Color(148, 0, 211)));
		setBackground(Color.WHITE);
		my_frame = the_frame;
		my_view = the_view;
		init();
		
	}
	
	/**
	 * Adds the specifications and components of the panel.
	 */
	private void init()
	{
		setLayout(null);
		
		cfList = new JComboBox<String>();
		cfList.setBounds(11, 44, 428, 33);
		cfList.setBackground(Color.WHITE);
		add(cfList);
		cfList.setFont(new Font("Urdu Typesetting", Font.PLAIN, 18));
		
		ArrayList<String> conferences = my_frame.db.getConferences();
		
		String[] conferenceArray = new String[conferences.size() + 1];
		conferenceArray[0] = "Select a Conference";
		for (int i = 0; i < conferences.size(); i++) {
			conferenceArray[i + 1] = conferences.get(i);

		}
		System.out.println("Conferences: " + conferences);
		cfList.setModel(new DefaultComboBoxModel(conferenceArray)); //Need to add in Conferences
		cfList.setSelectedIndex(0);

		JLabel paperTitleLabel = new JLabel("Paper Title: ");
		paperTitleLabel.setBounds(62, 152, 110, 24);
		add(paperTitleLabel);
		paperTitleLabel.setFont(new Font("Urdu Typesetting", Font.PLAIN, 18));
		paperTitleLabel.setHorizontalAlignment(SwingConstants.LEFT);
		
		JButton uploadButton = new JButton("Upload Paper");
		uploadButton.setBounds(255, 232, 166, 40);
		uploadButton.setFont(new Font("Urdu Typesetting", Font.PLAIN, 18));
		uploadButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {		
				if(!((String) cfList.getSelectedItem()).equals((String)cfList.getItemAt(0))) {
					JFileChooser fc = new JFileChooser();
					fc.showOpenDialog(fc);
					file = fc.getSelectedFile();
					try {
						uploadedFileName.setText(file.getName());
					} 
					catch (NullPointerException e) {
						
					}
					
				}
			}
		});
		add(uploadButton);
		
		JButton cancelButton = new JButton("Cancel");
		cancelButton.setToolTipText("Go back to previous screen");
		cancelButton.setBounds(231, 359, 118, 33);
		cancelButton.setFont(new Font("Urdu Typesetting", Font.PLAIN, 18));
		cancelButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				my_frame.setNewPanel(my_view);
				my_frame.repaint();
			}
		});
		add(cancelButton);
		
		JLabel lblNewLabel_1 = new JLabel("Prefer to be in pdf or doc format.");
		lblNewLabel_1.setBounds(143, 284, 288, 33);
		lblNewLabel_1.setFont(new Font("Times New Roman", Font.ITALIC, 14));
		add(lblNewLabel_1);
		
		uploadedFileName = new JLabel("");
		uploadedFileName.setBounds(35, 284, 251, 33);
		
		add(uploadedFileName);
		
		textField = new JTextField();
		textField.setBounds(185, 154, 277, 33);
		add(textField);
		textField.setColumns(10);
		final String paperName = textField.getText();
		
		JButton submitButton = new JButton("Submit");
		submitButton.setToolTipText("Submit this paper");
		submitButton.setBounds(74, 359, 135, 33);
		submitButton.setFont(new Font("Urdu Typesetting", Font.PLAIN, 18));
		submitButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				//Add paper to 
				if(!uploadedFileName.getText().equals("") && !textField.getText().equals("")) {
					try {
						CopyDocuments.copy(file, new File("Papers/" + textField.getText()));
					} catch (IOException e) {
						System.out.println("Submission Failed");
						e.printStackTrace();
					}
					final String paperName = textField.getText();
					ArrayList<String> authorPapers = my_frame.db.getAuthorPapersForConf(my_frame.currentUser[0][0], (String)cfList.getSelectedItem());
					boolean added = false;
					int i = 0; 
					while (!added) {
						if (authorPapers.get(i) == "") {
							authorPapers.set(i, paperName);
							added = true;
						}
						i++;
						if (i == 4)
							break;
					}
					if (!added && i >= 4) {
						JOptionPane.showMessageDialog(my_frame, "You cannot enter more than 4 papers to a conference!",
								"Error!", JOptionPane.ERROR_MESSAGE);
					} else {
						String conference = (String)cfList.getSelectedItem();
						String progChairName = my_frame.db.getProgChair(conference);
						my_frame.db.addPaper(my_frame.currentUser[0][0], (String)cfList.getSelectedItem(), authorPapers);
						my_frame.db.assignProgChair(progChairName, my_frame.currentUser[0][0], paperName);
						JOptionPane.showMessageDialog(my_frame, "Paper successfully submitted!", "Success!", JOptionPane.PLAIN_MESSAGE);
					}
				} else {
					JOptionPane.showMessageDialog(my_frame, "Error", "Error", JOptionPane.PLAIN_MESSAGE);
				}
			}
		});
		add(submitButton);
	}
}
